#include "ListItem.h"
#include <aknutils.h>
#include "ListBox.h"

CListItem* CListItem::NewL(const CListBox* aOwner)
	{
	CListItem* self = CListItem::NewLC(aOwner);
    CleanupStack::Pop(self);
    return self;
	}

CListItem* CListItem::NewLC(const CListBox* aOwner)
	{
	CListItem* self = new(ELeave) CListItem();
    CleanupStack::PushL(self);
    self->ConstructL(aOwner);
    return self;
	}

CListItem::CListItem()
	{
	iControlType = EItem;
	iTitle = NULL;
	iText = NULL;
	iBmpLeft = 5;
	iBmpTop = 3;
	iTitleFont = ApacPlain16();//CEikonEnv::Static()->SymbolFont();
	iTextFont = ApacPlain16();//CEikonEnv::Static()->SymbolFont();
	iTitleColor = KRgbBlack;
	iTextColor = KRgbBlack;
	iTextAlign = CGraphicsContext::ELeft;
	iPeriodic = NULL;
	iCurOffset = 0;
	iTextCurOffset = 0;
	}

CListItem::~CListItem()
	{
	delete iTitle;
	iTitle = NULL;
	delete iText;
	iText = NULL;
	if (iPeriodic && iPeriodic->IsActive())
		iPeriodic->Cancel();
	delete iPeriodic;
	iPeriodic = NULL;
	}

void CListItem::ConstructL(const CListBox* aOwner)
	{
	iOwner = aOwner;
	ActivateL();
	}

TKeyResponse CListItem::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
	{
	if (aType == EEventKey)
		{
		switch (aKeyEvent.iCode)
			{
			case EKeyEnter:
			case EKeyDevice3:
				return EKeyWasConsumed;
			}
		}
	return EKeyWasNotConsumed;
	}

void CListItem::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	CBaseControl::Draw(gc, aRect);
	TPoint position = Position();

    if (iTitle)
    	{
    	gc.UseFont(iTitleFont);
    	gc.SetPenColor(iTitleColor);
    	TRect titleRect(iTitlePosition, TPoint(Rect().iBr.iX - 2, iTitlePosition.iY + iTitleFont->HeightInPixels() + 4));
    	titleRect.Move(position);
    	TInt baseline = titleRect.Height() / 2 + iTitleFont->AscentInPixels() / 2;
    	gc.DrawText(iTitle->Mid(iCurOffset), titleRect, baseline, CGraphicsContext::ELeft, 0);
    	gc.DiscardFont();
    	}
    if (iText)
    	{
    	gc.UseFont(iTextFont);
    	gc.SetPenColor(iTextColor);
    	TRect textRect(iTextPosition, TPoint(Rect().iBr.iX - 2, iTextPosition.iY + iTextFont->HeightInPixels() + 4));
    	textRect.Move(position);
    	TInt baseline = textRect.Height() / 2 + iTextFont->AscentInPixels() / 2;
    	gc.DrawText(iText->Mid(iTextCurOffset), textRect, baseline, iTextAlign, 0);
    	gc.DiscardFont();
    	}
	}

void CListItem::DrawFocusEffect(CWindowGc& gc, const TRect& aRect) const
	{
    if (iOwner->SelItemBackGroundBitmap() && iOwner->SelItemBackGroundBitmap()->Bitmap())
        {
        TRect bmpPieceRect(TPoint(0, 0), iOwner->SelItemBackGroundBitmap()->SizeInPixels());
        if (iOwner->SelItemBackGroundBitmap()->MaskBitmap())
            gc.BitBltMasked(Position(),iOwner->SelItemBackGroundBitmap()->Bitmap(),bmpPieceRect,iOwner->SelItemBackGroundBitmap()->MaskBitmap(),EFalse);
        else
            gc.BitBlt(Position(), iOwner->SelItemBackGroundBitmap()->Bitmap(), bmpPieceRect);
        }
    else
        {
        //CBaseControl::DrawFocusEffect(gc, aRect);
        }
	}

void CListItem::DrawLostFocusEffect(CWindowGc& gc, const TRect& aRect) const
{

}

void CListItem::StartTimerL()
	{
	const TInt tickInterval=500000;
	if (!iPeriodic)
		iPeriodic=CPeriodic::NewL(0); // neutral priority
	if (!iPeriodic->IsActive())
		iPeriodic->Start(tickInterval,tickInterval,TCallBack(Tick, this));
	}

void CListItem::SizeChanged(const TRect& aRect)
	{
	TRect rect = Rect();
	rect.SetWidth(aRect.Width() - 2);
	SetRect(rect);
	}

void CListItem::FocusChanged(TDrawNow /*aDrawNow*/)
	{
	if (IsFocused())
	{
	    if (( iTitle && iTitleFont->TextWidthInPixels(*iTitle) > Rect().Width() - iTitlePosition.iX)
	            || ( iText && iTextFont->TextWidthInPixels(*iText) > Rect().Width() - iTextPosition.iX))
		{
		StartTimerL();
		}
	}
	else
		{
		if (iPeriodic && iPeriodic->IsActive())
			iPeriodic->Cancel();
		iCurOffset = 0;
		}
	}

TInt CListItem::Tick(TAny* aObject)
	{
	((CListItem*)aObject)->DoTick();
	 return 1;
	}

void CListItem::DoTick()
	{
    if (iTitle && iTitleFont->TextWidthInPixels(*iTitle) > Rect().Width() - iTitlePosition.iX)
    {
        iCurOffset++;
        if (iCurOffset >= iTitle->Length())
            iCurOffset = 0;
    }
	if (iText && iTextFont->TextWidthInPixels(*iText) > Rect().Width() - iTextPosition.iX)
	{
        iTextCurOffset++;
        if (iTextCurOffset >= iText->Length())
            iTextCurOffset = 0;
	}
	DrawDeferred();
	}

void CListItem::SetLayout()
	{
	TPoint beginPos(iBmpLeft,iBmpTop);
	if (iImage)
		beginPos.iX += iImage->SizeInPixels().iWidth;
	iTitlePosition.iX = beginPos.iX + 22;//title��ˮƽ���
    iTitlePosition.iY = iBmpTop/2;//title�Ĵ�ֱ���
    TInt height = iTitlePosition.iY + iTitleFont->HeightInPixels() + 1;
    if (iText)
    {
        iTextPosition.iX = iTitlePosition.iX + 5;//text��ˮƽ���
        iTextPosition.iY = iTitlePosition.iY +  + iTitleFont->HeightInPixels() + 5;//text�Ĵ�ֱ���
        height = iTextPosition.iY + iTextFont->HeightInPixels() + 1;
    }
    else
    {
		iTitlePosition.iY += iTitleFont->HeightInPixels() / 2; 
    }
	if (iOwner->SelItemBackGroundBitmap())
	{
		TInt bmpHeight = iOwner->SelItemBackGroundBitmap()->SizeInPixels().iHeight;
		if ( bmpHeight > height)
			height = bmpHeight;
	}
	TRect rect(0,0,iOwner->Rect().Width() - 9 ,height);
	SetRect(rect);
	}
//iImage�����ⲿ,��������ظ�ʹ��,�����ڴ�
void CListItem::SetBitmap(CImage* aImage)
    {
    iImage = aImage;
    }
//���ñ������ֵ�����
void CListItem::SetTitleFont(const CFont* aFont)
	{
	iTitleFont = aFont;
	}
//�����ı����ֵ�����
void CListItem::SetTextFont(const CFont* aFont)
	{
	iTextFont = aFont;
	}
//���ظ���ĸ߶�
TInt CListItem::ItemHeight()
	{
	return Rect().Height();
	}
//���ر�������
const HBufC* CListItem::Title()
	{
	return iTitle;
	}
//���ñ�������
void CListItem::SetTitleL(const TDesC& aTitle)
	{
	if (!(iTitle && iTitle->Compare(aTitle) == 0))
		{
		delete iTitle;
		iTitle = NULL;
		iTitle = HBufC::NewL(aTitle.Length());
		iTitle->Des().Append(aTitle);
		}
	}
//�����ı�����
const HBufC* CListItem::Text()
	{
	return iText;
	}
//�����ı�����
void CListItem::SetTextL(const TDesC& aText)
	{
	if (!(iText && iText->Compare(aText) == 0))
		{
		delete iText;
		iText = NULL;
		iText = HBufC::NewL(aText.Length());
		iText->Des().Append(aText);
		}
	}
//���ñ������ֵ���ɫ
void CListItem::SetTitleColor(TRgb aColor)
	{
	iTitleColor = aColor;
	}
//�����ı����ֵ���ɫ
void CListItem::SetTextColor(TRgb aColor)
	{
	iTextColor = aColor;
	}

void CListItem::SetTextAlign(CGraphicsContext::TTextAlign aAlign)
	{
	iTextAlign = aAlign;
	}
